package com.epoint.androidphone.mobileoa.ui.webinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import com.epoint.androidmobile.bizlogic.common.FileDownService;
import com.epoint.androidmobile.bizlogic.webinfo.model.AttachModel;
import com.epoint.androidmobile.bizlogic.webinfo.task.WebInfo_GetDetail_Task;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.init.InitView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author liyc
 * @time 2013-5-8 下午1:50:37
 * @annomation 信息阅读详细
 */
public class WebInfoDetailView extends SuperPhonePage {

	WebView wv;
	long getDetailTaskId;
	List<AttachModel> attList = new ArrayList<AttachModel>();
	String InfoGuid;
	Button btFj, btFk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.webinfodetailview, "信息详情");
		wv = (WebView) findViewById(R.id.wv);
		wv.setBackgroundColor(0);
		btFj = (Button) findViewById(R.id.btFj);
		btFj.setOnClickListener(this);
		btFk = (Button) findViewById(R.id.btFk);
		btFk.setOnClickListener(this);
		btFk.setVisibility(View.GONE);
		InfoGuid = getIntent().getStringExtra("InfoGuid");

		getBtRight().setText("操作");
		getBtRight().setOnClickListener(this);
		getBtRight().setVisibility(View.VISIBLE);

		HashMap<String, Object> params = getPassMap();
		params.put("InfoGuid", InfoGuid);
		getDetailTaskId = new WebInfo_GetDetail_Task(this, params).startTask();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == btFj) {
			if (attList.size() == 0) {
				return;
			}
			String[] attstr = new String[attList.size()];
			for (int i = 0; i < attList.size(); i++) {
				attstr[i] = attList.get(i).AttFileName;
			}
			AlertUtil.showAlertMenu(this, "附件下载", attstr,
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							AttachModel m = attList.get(which);
							FileDownService.download(getContext(),
									getString(R.string.WebServiceType),
									m.DownLoadUrl, m.isBigFile, InfoGuid,
									m.AttFileName, InitView.attachdownMainPath);
						}
					});
		} else if (v == getBtRight()) {
			AlertUtil.showAlertMenu(this, "操作选项", new String[] { "反馈信息" },
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							if(which==0){
								//jump to new activity
								// 跳转到反馈列表界面
								Intent intent = new Intent(
										WebInfoDetailView.this,
										WebInfoFeedBackListView.class);
								intent.putExtra("InfoGuid", InfoGuid);
								startActivity(intent);
							}
						}
					});
		}
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if (getDetailTaskId == taskId) {
			if (validateXML(obj)) {
				String bs = obj.toString();
				String title = getIntent().getStringExtra("Title");
				String PostUserName = getIntent()
						.getStringExtra("PostUserName");
				String PostDate = getIntent().getStringExtra("PostDate");
				String disContent = String
						.format("%s<div id='oschina_title'>%s</div><div id='oschina_outline'>%s %s</div><hr />",
								StringHelp.OschinaStyle, title, PostUserName,
								PostDate.split(" ")[0]);

				String infoType = StringHelp.getXMLAtt(bs, "infoProperty");

				if (infoType.equalsIgnoreCase("News")
						|| infoType.equalsIgnoreCase("Attach")) {
					String content = disContent
							+ StringHelp.get2AttMid(bs, "<content><![CDATA[",
									"]]></content>");
					wv.loadDataWithBaseURL(null, content, "text/html", "utf-8",
							null);
				} else if (infoType.equalsIgnoreCase("Link")) {
					Uri uri = Uri.parse(StringHelp.getXMLAtt(bs, "LinkUrl"));
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
					finish();
					return;
				}

				String AttachFilesXML = StringHelp.getXMLAttOut(bs,
						"AttachFiles");
				attList = XMLUtil.DomAnalysisCommon(AttachFilesXML,
						AttachModel.class);
				if (attList.size() > 0) {
					btFj.setText("附件(" + attList.size() + ")");
				}
			}
		}
	}

}
